package cn.lyh.qinfeng.plugin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.lyh.qinfeng.service.BaoMingService;
import cn.lyh.qinfeng.service.KaiTuanSerive;
import net.lz1998.cq.event.message.CQGroupMessageEvent;
import net.lz1998.cq.robot.CQPlugin;
import net.lz1998.cq.robot.CoolQ;
import net.lz1998.cq.utils.CQCode;

/**
 * @author lyh
 * @version 1.0
 * @date 2020/3/19
 * @Description 开团消息监控插件
 */
@Component
public class KaiTuanPlugin extends CQPlugin {
    @Autowired
    KaiTuanSerive kaiTuanSerive;

    @Autowired
    BaoMingService baoMingService;

    /**
     * 群消息插件
     *
     * @param cq    机器人对象
     * @param event 时间上报对象
     * @return 是否执行下一个插件
     */
    @Override
    public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event) {
        //获取群开团消息 开团  7.30ALD 1
        Object sender = event.getSender().getRole();
        int tuanNo;
        int gourpId = Math.toIntExact(event.getGroupId());
        ;
//       if ("owner".equals(sender) || "admin".equals(sender)) {
        String tuanexplain = event.getMessage().trim();
        System.out.println("tuanexplain = " + tuanexplain);
        if (tuanexplain.contains("开团") && tuanexplain.split(" ")[0].equals("开团")) {
            if ("owner".equals(sender) || "admin".equals(sender)) {
                tuanNo = Integer.parseInt(event.getMessage().trim().split(" ")[2]);
                // 开团结果返回值
                boolean kaituanRst = kaiTuanSerive.kaituan(tuanNo, tuanexplain, gourpId);
                if (kaituanRst) {
                    String result = CQCode.at(event.getUserId()) + "于" + tuanexplain + "开团成功";
                    cq.sendGroupMsg(gourpId, result, false);
                    return MESSAGE_BLOCK;
                }
            } else {
                String result = CQCode.at(event.getUserId()) + "您没有权限开团请联系管理员或者群主";
                cq.sendGroupMsg(gourpId, result, false);
            }
            cq.sendGroupMsg(gourpId, "开团失败应该有一个编号一样的团了！", false);
        } else if (tuanexplain.contains("取消开团") && tuanexplain.split(" ")[0].equals("取消开团")) {
            if ("owner".equals(sender) || "admin".equals(sender)) {
                tuanNo = Integer.parseInt(event.getMessage().trim().split(" ")[1]);
                gourpId = Math.toIntExact(event.getGroupId());
                if (kaiTuanSerive.deleteTuanIinfo(tuanNo) > 0) {
                    //如果返回值大于0则表示成功,取消开团了应该清空团队数据
                    baoMingService.deleteAllUserInfo(tuanNo);
                    cq.sendGroupMsg(gourpId, "团队" + tuanNo + "取消开团成功", false);
                }
            } else {
                String result = CQCode.at(event.getUserId()) + "您没有权限开团请联系管理员或者群主";
                cq.sendGroupMsg(gourpId, result, false);
            }
        }
        return MESSAGE_IGNORE;
    }


}
